﻿using System;


namespace BizElements.BusinessLayer
{
    /// <summary>
    /// The exception that is thrown when a business rule error was detected.
    /// </summary>
    public class BrokenRuleException : ApplicationException
    {
        #region Fields.

        BusinessRuleCollection brokenRules;

        #endregion

        #region Constructors.

        /// <summary>
        /// Initializes a new instances of the <b>BrokenRuleException</b> class.
        /// </summary>
        /// <param name="ruleName">Name of the rule which is not obeyed.</param>
        /// <param name="ruleDescription">Friendly description of the rule for the client.</param>
        public BrokenRuleException(string ruleName, FormattedMessage ruleDescription)
        {
            this.brokenRules = new BusinessRuleCollection();
            this.brokenRules.Add(new BusinessRule(ruleName, false, ruleDescription));
        }

        /// <summary>
        /// Initializes a new instances of the <b>BrokenRuleException</b> class.
        /// </summary>
        /// <param name="brokenRules">Broken business rules which caused the error.</param>
        public BrokenRuleException(BusinessRuleCollection brokenRules)
        {
            this.brokenRules = brokenRules;
        }

        /// <summary>
        /// Initializes a new instances of the <b>BrokenRuleException</b> class.
        /// </summary>
        /// <param name="brokenRule">A broken business rule which caused the error.</param>
        public BrokenRuleException(BusinessRule brokenRule)
        {
            this.brokenRules = new BusinessRuleCollection();
            this.brokenRules.Add(brokenRule);
        }

        #endregion

        #region ToString.

        /// <summary>
        /// Creates and returns a detailed infomation of the current exception.
        /// </summary>
        /// <returns>A string representation of the current exception.</returns>
        public override string ToString()
        {
            return this.brokenRules.ToString() + " " + base.ToString();
        }

        #endregion

        #region Properties.

        /// <summary>
        /// Gets a message that describes the current exception.
        /// </summary>
        public override string Message
        {
            get { return this.brokenRules.ToString(); }
        }

        /// <summary>
        /// Gets the broken business rules which caused the error.
        /// </summary>
        public BusinessRuleCollection BrokenRules
        {
            get { return this.brokenRules; }
        }

        #endregion
    }
}